(require :asdf)
(require :mel-base)

(defpackage :mel-examples
  (:use :mel :cl :mel.pop3-server))

(in-package :mel-examples)

(defparameter *inbox* (make-pop3-folder :host "mail.chase3000.com" 
					:username "sketerpot" 
					:password "uaukd"))

(defparameter *messages* (messages *inbox*))

(dolist (message *messages*)
  (format t "~A ~A~%" (from message) (subject message)))

(dolist (message *messages*)
  (multiple-value-bind (super sub) (content-type message)
    (format t "~A/~A ~A~%" super sub (from message))))

(defparameter *date* (field :date (first *messages*)))

(defparameter *parts* (parts (make-parts (find :multipart *messages*
					       :key #'content-type))))

(defparameter *part* (seventh *messages*))	; Adjust

(dolist (part *parts*)
  (multiple-value-bind (super sub) (content-type part)
    (format t "~A/~A" super sub)))

(close-folder *inbox*)

(defun get-text-part (message)
  (dolist (part (parts (make-parts message)))
    (multiple-value-bind (super sub) (content-type part)
      (print super) (print sub)
      (when (eq super :text)
	(case sub
	    (:plain (return-from get-text-part (values part 'text/plain)))
	    (:html (return-from get-text-part (values part 'text/html))))))))

(with-output-to-string (out)
   (with-open-stream (stream (message-body-stream (get-text-part *part*)))
     (loop for c = (read-char stream nil nil)
	   while c do (write-char c out))))